package dk.ucn.datamatiker.conceptgame;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ConceptGameMainActivity extends Activity {
	Context context;
	ArrayList<String> tasks;
	ArrayAdapter<String> adapter;
	ArrayList<String> words;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_concept_game_main);
		
		context = this;
		tasks = new ArrayList<String>();
		words = new ArrayList<String>();
		words.add("Christoffer");
		words.add("Christoffer");
		words.add("Christoffer");
		words.add("Christoffer");
		words.add("Christoffer");

		
		Button add = (Button)findViewById(R.id.addButton);
		Button game = (Button)findViewById(R.id.gameButton);
		final EditText editText = (EditText)findViewById(R.id.emailText);
		
		adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, tasks);
		ListView listView = (ListView)findViewById(R.id.emailList);
		listView.setAdapter(adapter);
		
		add.setOnClickListener(new View.OnClickListener() 
		{
			@Override
			public void onClick(View view)
			{
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.showSoftInput(editText, 0);
				
				String value = editText.getText().toString();
				if(validEmail(value) == true)
				{
					tasks.add(value);
					
					adapter.notifyDataSetChanged();
					editText.setText("");
				}
				else
				{
					AlertDialog.Builder alert = new AlertDialog.Builder(context);
					
					alert.setIcon(R.drawable.ic_launcher);
					alert.setTitle("FEJL!");
					alert.setMessage("Du har skrevet E-mailen forkert, tilbage til folkeskolen med dig!");
					alert.setPositiveButton(":-(", new DialogInterface.OnClickListener()
					{
						
						@Override
						public void onClick(DialogInterface dialog, int which) 
						{
							
						}
					});
					alert.show();
					editText.setText("");
				}
			}
	});
		
		game.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				
				
				Intent Imail = new Intent(ConceptGameMainActivity.this, Mail.class);
				Imail.putExtra("Mail", tasks);
				Imail.putExtra("Words", words);
				
				startActivity(Imail);
			
				
			}
		});
}
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.concept_game_main, menu);
		return true;
	}
	
	private boolean validEmail(String email)
	{
		Pattern pattern = Patterns.EMAIL_ADDRESS;
		return pattern.matcher(email).matches();
	}

}
